package com.yc.blog.dao.impl;

import java.util.List;

import com.yc.blog.bean.BlogInfo;
import com.yc.blog.dao.DBHelper;
import com.yc.blog.dao.IBlogInfoDao;

public class BlogInfoDaoImpl implements IBlogInfoDao{

	@Override
	public int add(String tid, String title, String content, int aid) {
		DBHelper db = new DBHelper(); 
		String sql = "insert into bloginfo values(0, ?, ?, ?, ?, now(), 0, 0, 0)";
		return db.update(sql, aid, tid, title, content);
	}

	@Override
	public List<BlogInfo> findByPage(int page, int rows) {
		DBHelper db = new DBHelper(); 
		String sql ="select bid, b.aid, b.tid, aname as nickName, photo, tname, title, left(content, instr(content, '</p>') + 4) content,"
					+ " date_format(bdate, '%Y-%m-%d %H:%i') bdate, collect, "
					+"support, oppose from bloginfo b, blogtype t, admininfo a where b.aid=a.aid and b.tid=t.tid order by bid desc limit ?, ?";
		return db.finds(BlogInfo.class, sql, (page - 1) * rows, rows);
	}

	@Override
	public BlogInfo findByBid(String bid) {
		DBHelper db = new DBHelper(); 
		String sql = "select bid, b.aid, b.tid, aname as nickName, tname, title, content, date_format(bdate, '%Y-%m-%d %H:%i') bdate,"
					+ " collect, support, oppose from bloginfo b, blogtype t, admininfo a where b.aid=a.aid and b.tid=t.tid and bid=?";
		return db.find(BlogInfo.class, sql, bid);
	}

}
